/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ticketonline.ui.servlet;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import ticketonline.entity.Customer;
import ticketonline.service.AppService;

/**
 *
 * @author amir
 */
public class EditCustomer extends HttpServlet{

    private AppService service;
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        ApplicationContext ctx = WebApplicationContextUtils
        .getWebApplicationContext(config.getServletContext());

        service = (AppService) ctx.getBean("appService");
    }

    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        
        String id = req.getParameter("id");
        if(id==null) id="";
		
        String output = "<html>";
        output += "<head>";
        output += "<title>Daftar Grup</title>";
        output += "</head>";
        output += "<body>";
        output += "<h1>Edit Grup</h1>";

        output += "<form method='post' action='simpan'>";
        output += "<table>";
        output += "<tr>";
        output += "<td>ID</td>";
        output += "<td><input type='text' name='id' readonly='true' value='"+ id +"'></td>";
        output += "</tr>";
        
        output += "<tr>";
        output += "<td>Name</td>";
        output += "<td><input type='text' name='name'></td>";
        output += "</tr>";
        
        output += "<tr>";
        output += "<td>Address</td>";
        output += "<td><input type='text' name='address'></td>";
        output += "</tr>";

        output += "<tr>";
        output += "<td>Phone</td>";
        output += "<td><input type='text' name='phone'></td>";
        output += "</tr>";

        output += "<tr>";
        output += "<td>&nbsp;</td>";
        output += "<td><input type='submit' value='Simpan'></td>";
        output += "</tr>";


        output += "</table>";
        output += "</form>";

        output += "</body>";
        output += "</html>";

        resp.getWriter().print(output);
        resp.getWriter().close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        
        Customer c = new Customer();
        
        if(!id.equals("")) c.setCustID(new Integer(id));
        c.setCustName(name);
        c.setCustAddress(address);
        c.setCustPhone(phone);
        
        service.saveCustomer(c);
        resp.sendRedirect("daftar.dot");
    }
    
    
    
}
